/**
 * Created by zkt on 2017/1/17.
 */
var menuModel = require('../../model/Menu');
var ejs = require('ejs');
var util = require("util");
var page = require('../../config/page');
// 格式化输出日期
ejs.filters.dateFormat = function (time) {
    return new Date(time).toLocaleString().replace(/T/, ' ').replace(/\..+/, '');
};
module.exports = function(app){
    app.get('/menu/index',function(req,res){

        var pageSize = req.query.pageSize || 10;
        var pageNum = req.query.pageNum || 1;

        var menuName = req.query.menuName;
        var menuType = req.query.menuType;
        var hasSubMenu = req.query.hasSubMenu;
        var params = {condition:{},pageInfo:{pageNum:pageNum,pageSize:pageSize}};
        if(!util.isNullOrUndefined(menuName)){
            params.condition.menu_name = {like : '%'+menuName+'%'};
        }
        if(!util.isNullOrUndefined(menuType)){
            params.condition.menu_type = menuType;
        }
        menuModel.getListPage(params,function (error,result) {
            if(error){
                console.log('查询出错！');
                throw error;
            }else{
                var totalCount = result.count;
                var pageModel = page.pageInit(totalCount,pageSize,pageNum);
                var data = result.rows;
                res.render('menu/listMenu', {result: {data:data,page:pageModel}});
            }
        });

    });
    app.get('/menu/add',function(req,res){
        var params = {menu_pid:0,menu_type:1};
        menuModel.getListByParams(params,function(error,result){
            var menu_parent_list = [];
            if(!error){
                menu_parent_list = result;
            }
            res.render('menu/addMenu',{result:menu_parent_list});
        });
    });
    app.post('/menu/save',function(req,res){
        var menu = req.body.menu;
        if(null == menu){
            res.json({result:0});
        }
        menuModel.save(menu,function(err,result){
            if(err){
                console.log('保存出错！');
                res.json({result:0});
            }else{
                if(result.id && result.id > 0){
                    res.json({result:1});
                }
            }
        });
    });
    app.get('/menu/edit',function(req,res){
        var id = req.query.id;
        if(null == id){
            res.render('menu/index');
        }
        menuModel.getMenuById(id,function(err,result){
            if(err){
                console.log('保存出错！');
                res.render('menu/index');
            }else{
                if(result.id && result.id > 0){
                    res.render('menu/editMenu',{result:result});
                }
            }
        });
    });
    app.post('/menu/update',function(req,res){
        var menu = req.body.menu;
        if(null == menu){
            res.json({result:0});
        }
        menuModel.update(menu,function(err,result){
            if(err){
                console.log('修改出错！');
                res.json({result:0});
            }else{
                if(result > 0){
                    res.json({result:1});
                }
            }
        });
    });
    app.post('/menu/delete',function(req,res){
        var id = req.body.id;
        if(null == id){
            res.json({result:0});
        }
        menuModel.delete(id,function(err,result){
            if(err){
                console.log('删除出错！');
                res.json({result:0});
            }else{
                if(result > 0){
                    res.json({result:1});
                }
            }
        });
    });
};